package cn.maiyar.generator.entity;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;

/**
 * 自定义数据源
 *
 * @author c.c
 */
@Data
public class CustomDataSource implements Serializable {
    // 数据库名称
    private String name = "mysql";
    // 数据库驱动名
    private String driverName;
    // 数据库链接地址
    private String url;
    // 数据库用户名
    private String username;
    // 数据库密码
    private String password;

    public CustomDataSource() {
        Properties properties = this.getProperties();
        this.driverName = StrUtil.toString(properties.getProperty("spring.datasource.driver-class-name"));
        this.url = StrUtil.toString(properties.getProperty("spring.datasource.url"));
        this.username = StrUtil.toString(properties.getProperty("spring.datasource.username"));
        this.password = StrUtil.toString(properties.getProperty("spring.datasource.password"));
    }

    /**
     * 获取配置文件
     *
     * @return 配置Props
     */
    private Properties getProperties() {
        // 读取配置文件
        Resource resource = new ClassPathResource("/templates/code.properties");
        Properties props = new Properties();
        try {
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }
}
